package com.test.example.controller.excel;

import com.test.example.controller.excel.model.*;
import com.test.example.controller.excel.style.ExcelSimpleStyler;
import com.xingluo.tools.excel.ExcelUtils;
import com.xingluo.tools.result.Result;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.*;

@RestController
public class ExcelController {

    /**
     * 导出简单表
     */
    @PostMapping(value = "/export1")
    public void export1(HttpServletResponse response){
        // 不用表标题，默认sheet名，只要文件名
        ExcelUtils.exportExcel(mockData1(), "测试用户表", response);
    }

    /**
     * 导出简单表，添加样式
     */
    @PostMapping(value = "/export2")
    public void export2(HttpServletResponse response){
        // 不用表标题，默认sheet名，只要文件名
        ExcelUtils.exportExcel(mockData1(), "用户信息","表1","测试用户表", ExcelSimpleStyler.class, response);
    }


    /**
     * 导出一对多对象的复杂表
     */
    @PostMapping(value = "/export3")
    public void export3(HttpServletResponse response){
        // 导出数据、表标题、表sheet名、文件名、输出流
        ExcelUtils.exportExcel(mockData2(), "学生选课","选课表","课程", response);
    }

    /**
     * 导出多张表的一个Excel文件
     */
    @PostMapping(value = "/export4")
    public void export4(HttpServletResponse response){
        List<Map<String, Object>> list = new ArrayList<>();
        list.add(ExcelUtils.createSheet(mockData1()));  // 没有表标题、默认表名：Sheet0
        list.add(ExcelUtils.createSheet("学生选课列", "学生选课表", mockData2()));
        ExcelUtils.exportMapExcel(list, "测试用户", response);
    }

    /**
     * 用模版导出
     */
    @PostMapping(value = "/export5")
    public void export5(HttpServletResponse response){
        Map<String, Object> map = new HashMap<>();
        map.put("list", mockData1());
        ExcelUtils.templateExport("excel/test1.xlsx", map, "测试用户", response);
    }

    /**
     * 用模版导出
     */
    @PostMapping(value = "/export6")
    public void export6(HttpServletResponse response){
        Map<String, Object> map = new HashMap<>();
        map.put("list", mockData3());
        ExcelUtils.templateExport("excel/test4.xlsx", map, "测试订单", response);
    }

    /**
     * 导入excel
     */
    @PostMapping(value = "/import")
    public Result importStudent(MultipartFile file) throws Exception{
        // titleRows表格标题的行数，如果没有就是0，如果有一个标题就是1，如果是两个标题就2
        // headRows表示的是表头行数，默认是1，如果有两个表头则需要设置2。
        List<Course> courses = ExcelUtils.importExcel(file, 1, 2, Course.class);
        return Result.success(courses);
    }

    public List<User> mockData1(){
        List<User> users = new ArrayList<>();
        users.add(new User(1, "用户1", "password1", 25, 1, new Date()));
        users.add(new User(2, "用户2", "password2", 30, 1, new Date()));
        users.add(new User(3, "用户3", "password3", 22, 0, new Date()));
        users.add(new User(4, "用户4", "password4", 28, 1, new Date()));
        users.add(new User(5, "用户5", "password5", 35, 0, new Date()));
        return users;
    }

    public List<Course> mockData2(){
        // 创建学生对象
        Student student1 = new Student("1", "张三", 1, "https://b0.bdstatic.com/25d3418b43d28432160f6cea27fae259.jpg@h_1280", new Date(), new Date());
        Student student2 = new Student("2", "李四", 2, "https://b0.bdstatic.com/25d3418b43d28432160f6cea27fae259.jpg@h_1280",new Date(), new Date());
        Student student3 = new Student("3", "王五", 1, "https://b0.bdstatic.com/25d3418b43d28432160f6cea27fae259.jpg@h_1280",new Date(), new Date());
        Student student4 = new Student("4", "赵六", 2, "https://b0.bdstatic.com/25d3418b43d28432160f6cea27fae259.jpg@h_1280",new Date(), new Date());
        Student student5 = new Student("5", "钱七", 1, "https://b0.bdstatic.com/25d3418b43d28432160f6cea27fae259.jpg@h_1280",new Date(), new Date());

        // 创建老师对象
        Teacher teacher1 = new Teacher("t1", "王老师");
        Teacher teacher2 = new Teacher("t2", "邱老师");

        // 创建课程对象并添加学生
        Course course1 = new Course("c1", "数学", teacher1, new ArrayList<>(Arrays.asList(student1, student2)));
        Course course2 = new Course("c2", "英语", teacher2, new ArrayList<>(Arrays.asList(student3, student4, student5)));

        // 创建课程列表
        List<Course> courses = new ArrayList<>();
        courses.add(course1);
        courses.add(course2);
        return courses;
    }

    public static List<Orders> mockData3() {
        List<Orders> ordersList = new ArrayList<>();

        // 订单 1
        Orders order1 = new Orders();
        order1.setId(1);
        order1.company = "北京电子商贸公司下单表";
        order1.createTime = "下单日期：2024.10.28";
        order1.operator = "业务员：张三";
        order1.tel = "客户电话：010-12345678";
        order1.address = "客户地址：北京市朝阳区望京街道123号";
        order1.remark = "备注：尽快发货";
        order1.items = Arrays.asList(
                new OrderItem(1,"笔记本电脑", "联想", "LAP123", "台", 2, "尽快发货"),
                new OrderItem(2,"鼠标", "罗技", "MOU456", "个", 5, "普通配送"),
                new OrderItem(3,"键盘", "罗技", "KEY789", "个", 3, "含批量折扣")
        );

        // 订单 2
        Orders order2 = new Orders();
        order2.setId(2);
        order2.company = "上海家居商贸有限公司下单表";
        order2.createTime = "下单日期：2024.10.28";
        order2.operator = "业务员：李四";
        order2.tel = "客户电话：021-98765432";
        order2.address = "客户地址：上海市浦东新区世纪大道456号";
        order1.remark = "备注：尽快发货";
        order2.items = Arrays.asList(
                new OrderItem(1,"智能手机", "三星", "SAM123", "部", 10, "加急配送"),
                new OrderItem(2,"平板电脑", "苹果", "TAB456", "台", 5, "普通配送"),
                new OrderItem(3,"耳机", "索尼", "EAR789", "副", 20, "批量优惠")
        );

        ordersList.add(order1);
        ordersList.add(order2);

        return ordersList;
    }
}
